|
|
 |
|
|
|
|
13: Description:=Something's wrong here
14: End If
15: ' (Run method code continues.)
16: End Sub |
|
|
|
|
|
|
|
|
When you test preconditions such as the validity of argument values, you can simply place the Err.Raise statements (similar to Listing 18.4) at the beginning of the method, just before the first On Error statement. |
|
|
|
|
|
|
|
|
When you are using references to foreign objects owned by other components, it is important to handle errors that can result when you invoke methods of those objects. For as many errors as may be generated by the owning component, your component must have access to a list of such errors from the owning component's documentation. This is especially important as you want to gracefully handle errors of foreign objects on behalf of your component's client application. After all, your client application only knows your component and has been developed to handle your component's errors. It wouldn't make sense to pass along errors from your foreign objects to the client application. This would break encapsulation as your client application would then have intimate details of what your component does internally. For commercial development especially, this is unacceptable. |
|
|
|
|
|
|
|
|
Listing 18.5 shows a snippet of code that uses an error handler for components that use foreign objects owned by another component. Upon encountering an error beyond its ability to properly enforce, the error handler raises an error for the client, complete with its own error numbers and descriptions. In other words, your component becomes a helpful translator for the foreign objects, just as a human language translator translates the words of foreigners. |
|
|
|
|
|
|
|
|
LISTING 18.5. USING THERaiseMETHOD FOR ERRORS ORIGINATING WITH FOREIGN OBJECTS. |
|
|
|
 |
|
|
|
|
1: Public Sub doSomething(argSpeed As Double)
2: On Error Goto doSomethingErr
3: ' Some code goes here. . .
4: '
5: Exit Sub
6: doSomethingErr:
7: Select Case Err.Number
8: ' Errors your component can handle would go here
9: '
10: ' --- Errors from some foreign component. ---
11: Case vbObjectError + SOME_FOREIGN_ERR_NUMBER
12: Err.Raise TRANSLATED_NUMBER + vbObjectError, _
13: COMPONENT_SOURCENAME, _
|
|
|
|
|
|